/*
 * MainStatusBar.java
 * 
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.elements;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.lailaps.gui.GUIBuilder;
import org.tiberiumlabs.lailaps.resources.ImageLoader;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class MainStatusBar extends AbstractStatusBar {
    
    public MainStatusBar() {
        super();
        
        statusTimer.setInitialDelay(800);
        statusTimer.setDelay(50);
        
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        
        // <editor-fold defaultstate="collapsed" desc=" fonts ">
        statusLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 11));
        Font font = PLAIN_FONT_11;
        stoppedLabel.setFont(font);
        workingLabel.setFont(font);
        completedLabel.setFont(font);
        totalLabel.setFont(font);
        // </editor-fold>
        
        // <editor-fold defaultstate="collapsed" desc=" icons ">
        // TODO find better icons
        workingLabel.setIcon(ImageLoader.getStatusWorkingIcon());
        completedLabel.setIcon(ImageLoader.getStatusCompletedIcon());
        totalLabel.setIcon(ImageLoader.getStatusListIcon());
        stoppedLabel.setIcon(ImageLoader.getStatusStoppedIcon());
        // </editor-fold>
        
        // TODO better than set maximum label size - create internal panel 
        // with 4 labels and set maximum size to that panel
        // <editor-fold defaultstate="collapsed" desc=" label size ">
        Dimension dimension = totalLabel.getPreferredSize();
        totalLabel.setMaximumSize(dimension);
        workingLabel.setMaximumSize(dimension);
        stoppedLabel.setMaximumSize(dimension);
        completedLabel.setMaximumSize(dimension);
        // </editor-fold>
        
        add(statusLabel);
        add(Box.createHorizontalGlue());
        add(workingLabel);
        add(Box.createHorizontalStrut(8));
        add(stoppedLabel);
        add(Box.createHorizontalStrut(8));
        add(completedLabel);
        add(Box.createHorizontalStrut(8));
        add(totalLabel);
        add(Box.createHorizontalStrut(20));
        
        setBorder(new EtchedBorder() {

            @Override
            public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                Color oldColor = g.getColor();

                int w = width - 1;

                g.translate(x, y);

                g.setColor(getShadowColor(c));
                g.drawLine(0, 0, w, 0);

                g.setColor(getHighlightColor(c));
                g.drawLine(0, 1, w, 1);

                g.translate(-x, -y);

                g.setColor(oldColor);
            }
        });
        
    }
    
    // <editor-fold defaultstate="collapsed" desc=" status message ">
    
    // TODO here is fade out logic, invoked about 10 times per second. may be better without?
    
    private final JLabel statusLabel = new JLabel();
    
    public void setStatusLabel(String text) {
        statusLabel.setText(text);
        statusLabel.setToolTipText(text);
        statusLabel.setForeground(defaultForeground);
        statusTimer.restart();
        currentStep = 0;
    }
    
    public static void setStatusMessage(String text) {
        GUIBuilder guiBuilder = Mediator.getMediator().getGuiBuilder();
        if (guiBuilder.isMainWindowVisible()) {
            guiBuilder.getMainStatusBar().setStatusLabel(text);
        }
    }
    
    private final Timer statusTimer = new Timer(500, new StatusActionListener());
    private Color defaultForeground = statusLabel.getForeground();
    
    private Color[] fadeColors = null;
    private int currentStep = 0;
    
    private void fadeOutStatusLabel() {
        if (fadeColors == null) {
            
            int colorsLength = 20;
            
            Color background = getBackground();
            
            int br = background.getRed();
            int bg = background.getGreen();
            int bb = background.getBlue();
            
            int fr = defaultForeground.getRed();
            int fg = defaultForeground.getGreen();
            int fb = defaultForeground.getBlue();
            
            int sr = (fr - br)/colorsLength;
            int sg = (fg - bg)/colorsLength;
            int sb = (fb - bb)/colorsLength;
            
            fadeColors = new Color[colorsLength];
            
            fadeColors[0] = defaultForeground;
            
            for (int i = 1; i < colorsLength; i++) {
                fr = fr - sr;
                fg = fg - sg;
                fb = fb - sb;
                fadeColors[i] = new Color(fr, fg, fb);
            }
        }
        
        if (currentStep == 20) {
            currentStep = 0;
            statusTimer.stop();
            statusLabel.setText(null);
            statusLabel.setForeground(defaultForeground);
            return;
        }
        statusLabel.setForeground(fadeColors[currentStep]);
        currentStep++;
    }
    
    private class StatusActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            fadeOutStatusLabel();
        }
    }
    
    // </editor-fold>
    
}
